package cn.tedu.ivos.application.service.impl;

import cn.tedu.ivos.application.mapper.ApplicationMapper;
import cn.tedu.ivos.application.pojo.dto.ApplicationQuery;
import cn.tedu.ivos.application.pojo.dto.ApplicationSaveParam;
import cn.tedu.ivos.application.pojo.entity.Application;
import cn.tedu.ivos.application.pojo.vo.ApplicationVO;
import cn.tedu.ivos.application.service.ApplicationService;
import cn.tedu.ivos.audit.mapper.AuditMapper;
import cn.tedu.ivos.audit.pojo.vo.AuditVO;
import cn.tedu.ivos.audit.service.AuditService;
import cn.tedu.ivos.base.enums.ApplicationStatusEnum;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.dto.UserQuery;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Slf4j
@Transactional
@Service
public class ApplicationServiceImpl implements ApplicationService {

    @Autowired
    ApplicationMapper applicationMapper;

    @Autowired
    AuditService auditService;
    @Autowired
    UserMapper userMapper;
    @Autowired
    AuditMapper auditMapper;
    @Autowired
    VehicleMapper vehicleMapper;


    @Override
    public void saveApplication(ApplicationSaveParam applicationSaveParam) {
        log.debug("新增申请单业务,参数:{}",applicationSaveParam);
        Application application=new Application();
        BeanUtils.copyProperties(applicationSaveParam,application);
//        设置当前申请单的状态为发起
        application.setStatus(ApplicationStatusEnum.PENDING.getCode());
        application.setCreateTime(new Date());
//        遇到的问题:新增申请时,对应审批单数据中没有当前申请单的id;
//        原因:传给审批的申请对象application里id没有值
//        解决方案:给此方法对应的SQL加上useGenerateKeys="true" keyProperty="id"




        applicationMapper.insert(application);
//        还需要为此申请单生成对应的审批单数据
        auditService.insertAudit(application);



    }

    @Override
    public List<ApplicationVO> selectApplication(ApplicationQuery applicationQuery) {
        log.debug("查询申请单业务,参数:{}",applicationQuery);
        List<ApplicationVO> list=applicationMapper.selectApplication(applicationQuery);
        log.debug("返回的申请单:参数{}",list);

//      遍历每一个查询出来的申请单对象,依次为每个申请单补充审批人数据
        for(int i=0;i<list.size();i++){
            ApplicationVO applicationVO=list.get(i);
//            遍历到当前申请单,给他补充审批人的数据
            assignAudiUserList(applicationVO);
        }

        return list;

    }

    @Override
    public void cancel(Long id) {
        log.debug("取消申请业务,参数:{}",id);
        Application application=new Application();
        application.setId(id);
        application.setStatus(ApplicationStatusEnum.CANCEL.getCode());
        application.setUpdateTime(new Date());
        applicationMapper.update(application);
//        其次删除申请单对应的审批单数据,这里的id是申请单的id
        auditMapper.deleteAuditByApplicationId(id);

    }

    @Override
    public void distribute(Long applicationId, Long vehicleId) {
        log.debug("分配车辆业务，参数{}，{}",applicationId,vehicleId);
        Application application=new Application();
        application.setId(applicationId);
        application.setVehicleId(vehicleId);
        application.setUpdateTime(new Date());
        application.setStatus(ApplicationStatusEnum.ALLOCATION.getCode());
        applicationMapper.update(application);


        Vehicle vehicle=new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setStatus("2");
        vehicle.setUpdateTime(new Date());
        vehicleMapper.updateVehicle(vehicle);
    }

    @Override
    public void backVehicle(Long applicationId, Long vehicleId) {
        log.debug("还车业务，参数：{},{}",applicationId,vehicleId);
        Application application=new Application();
        application.setId(applicationId);
        application.setStatus(ApplicationStatusEnum.END.getCode());
        application.setUpdateTime(new Date());
        applicationMapper.update(application);

        Vehicle vehicle=new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setStatus("1");
      vehicle.setUpdateTime(new Date());
        vehicleMapper.updateVehicle(vehicle);
    }


    private void assignAudiUserList(ApplicationVO applicationVO) {
//        1.准备空集合用来保存申请单对应的审批人姓名与id数据
        List<String> auditUsernameList= new ArrayList<>();
        List<Long> auditUserIdList=new ArrayList<>();
//        2.根据传入的申请单数据,获取当前申请单id,查询此申请单对应的审批单数据
//        注意,此查询出的审批单可能有多条数据
        List<AuditVO> auditVOList=auditService.selectAuditByApplicationId(applicationVO.getId());
//        3.依次遍历审批单数据,获取审批人id与姓名
        for(int i=0;i<auditVOList.size();i++){
            AuditVO auditVO=auditVOList.get(i);
//            4.取出当前审批单的审批人id
            Long auditUserId=auditVO.getAuditUserId();
            auditUserIdList.add(auditUserId);
//            5.根据审批人id到用户模块查询审批人姓名,并装入集合
            UserVO userVO=userMapper.selectUserById(auditUserId);
            auditUsernameList.add(userVO.getUsername());
        }
//        6.准备一个拼接工具,帮我们将所有审批人姓名连城一个字符串
        StringJoiner stringJoiner=new StringJoiner(",");
        for (String username:auditUsernameList){
            stringJoiner.add(username);
        }
//        将拼接好的姓名字符串赋值给传入申请单对象
        applicationVO.setAuditUsernameList(stringJoiner.toString());
//        将准备好的审批人id集合赋值到传入申请单对象的属性
        applicationVO.setAuditUserIdList(auditUserIdList);



    }
}
